package com.example.demo.concurrent;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * Created by alva on 18/7/4.
 */
public class CallableAndFuture {
    public static void main(String[] args) {
        Callable<Integer> callable =  new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return new Random().nextInt();
            }
        };

        FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);
        new Thread(futureTask).start();
        try{
            Thread.sleep(1000);
            int result = futureTask.get(Long.parseLong("10"), TimeUnit.SECONDS);
            System.out.printf("result : "+result);
        }catch (InterruptedException e){
            e.printStackTrace();
        }catch (Exception ee ){
            ee.printStackTrace();
        }

    }

}
